/**
 * Categories for pictures
 */
public class Category {
	private static final String CATEGORYRB = "grails-app.i18n.categoryRB"
	private static final List<String> categoryKeys = null
	String internalValue = null
	String displayValue = null
	
	public static Category toCategory(String name) {
		Category category = new Category()
		List<String> internalValues = getInternalValues()
		if(internalValues.contains(name)) {
			category.internalValue = name
			return category
		}
		return category		
	}
	
	public String getDisplayValue(Locale locale) {
		ResourceBundle categoryRB = ResourceBundle.getBundle(CATEGORYRB, locale)
		return categoryRB.getString(internalValue)
	}
	
	public static List<String> getInternalValues() {
		if(categoryKeys == null) {
			ResourceBundle messages = ResourceBundle.getBundle(CATEGORYRB);
			categoryKeys = messages.getKeys().toList()
		} 
		return categoryKeys
	}
	
	public static List<String> getDisplayValues(Locale locale) {
		ResourceBundle categoryRB = ResourceBundle.getBundle(CATEGORYRB, locale)
		List<String> displayValues = new ArrayList<String>()
		List<String> internalValues = getInternalValues()
		for(String key:internalValues) {
			displayValues.add(categoryRB.getString(key))
		}
		return displayValues;
	}
	
	public static List<Category> getCategoryMap(Locale locale) {
		ResourceBundle categoryRB = ResourceBundle.getBundle(CATEGORYRB, locale)
		List<Category> categoryList = new ArrayList<Category>()
		List<String> displayValues = new ArrayList<String>()
		List<String> internalValues = getInternalValues()
		for(String key:internalValues) {
			Category category = new Category()
			category.internalValue = key
			category.displayValue = categoryRB.getString(key)
			displayValues.add(category.displayValue)
			categoryList.add(category)
		}
		List<String> sortedDisplayValues = new ArrayList<String>(displayValues)
		Collections.sort(sortedDisplayValues)
		List<Category> sortedCategoryList = new ArrayList<Category>(categoryList.size())
		for(String display:sortedDisplayValues) {
			int idx = displayValues.indexOf(display)
			sortedCategoryList.add(categoryList.get(idx))
		}		
		return sortedCategoryList
	}
}
